package cn.tedu.charging.order.config;

import cn.tedu.charging.common.constant.AmqpComponentConst;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@Slf4j
public class AmqpDeclareConfiguration {
    //1.业务交换机,保证绑定简单 fanout类型
    @Bean
    public Exchange delayBillEx(){
        return ExchangeBuilder.fanoutExchange(AmqpComponentConst.DELAY_BILL_EX).build();
    }

    //2.业务队列 只做消息暂时存储
    @Bean
    public Queue delayBillQueue(){
        return QueueBuilder.nonDurable(AmqpComponentConst.DELAY_BILL_QUEUE)
                .ttl(1000*60)//1分钟延迟
                .deadLetterExchange(AmqpComponentConst.DELAY_BILL_DL_EX)
                .deadLetterRoutingKey(AmqpComponentConst.DELAY_BILL_DL_RK)
                .build();
    }
    //3.业务队列和交换机绑定
    @Bean
    public Binding delayBillBind(){
        return BindingBuilder.bind(delayBillQueue()).to(delayBillEx()).with("").noargs();
    }

    //4.死信交换机
    @Bean
    public Exchange delayBillDlEx(){
        return ExchangeBuilder.directExchange(AmqpComponentConst.DELAY_BILL_DL_EX).build();
    }
    @Bean
    public Queue delayBillDlQueue(){
        return QueueBuilder.nonDurable(AmqpComponentConst.DELAY_BILL_DL_QUEUE).build();
    }
    @Bean
    public Binding delayBillDlBinding(){
        return BindingBuilder.bind(delayBillDlQueue()).to(delayBillDlEx())
                .with(AmqpComponentConst.DELAY_BILL_DL_RK).noargs();
    }

}
